﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class BillDetailController : Controller
    {
        // view login
        public ActionResult index()
        {
            string[] Bill;
            string[][] BillDetail;
            BillModel bm = new BillModel();
            BillDetailModel bdm = new BillDetailModel();
            int bid = 0;
            if (this.Session["Session_Admin"] == null || this.Session["Admin_Type"] == null || Convert.ToInt32(this.Session["Admin_Type"].ToString()) != 1)
            {
                Response.Redirect("/admin/login");
            }
            if ((this.Request.QueryString["bid"] != null) && (this.Request.QueryString["bid"] != ""))
            {
                try
                {
                    bid = Convert.ToInt32(this.Request.QueryString["bid"]);
                }
                catch (SystemException se)
                {
                    Response.Redirect("/admin/home");
                }
            }
            else
            {
                Response.Redirect("/admin/home");
            }
            Bill =  bm.getBillById(bid);
            BillDetail = bdm.getBillDetailByBillId(bid);
            if ((Bill[0] == "") || (Bill[0] == null) || (BillDetail[0][0] == "") || (BillDetail[0][0] == null))
            {
                Response.Redirect("/admin/home");
            }
            try
            {
                int success = Convert.ToInt32(this.Session["success"]);
                if (success == 1 || success == 2)
                {
                    this.Session["success"] = null;
                    ViewData["success"] = success;
                }
            }
            catch (SystemException se)
            {
                Response.Redirect("/home/index");
            }
            ViewData["Bill"] = Bill;
            ViewData["Bill_Detail"] = BillDetail;
            ViewData["bill_id"] = bid;
            return View();
        }

        public ActionResult updatestatus()
        {
            BillModel Bm = new BillModel();
            BookModel book = new BookModel();
            BillDetailModel Bdm = new BillDetailModel();
            int id = 0;
            int err = 0;
            if ((this.Request.QueryString["bid"] != null) && (this.Request.QueryString["bid"] != "") && (this.Request.Form["status"] != null) && (this.Request.Form["status"] != null))
            {
                try
                {
                    id = Convert.ToInt32(this.Request.QueryString["bid"].ToString());
                    int status = Convert.ToInt32(this.Request.Form["status"].ToString());
                    err = Bm.updateBillStatus(id, status);
                    if (status == 3)
                    {
                        string check = Bm.getCheckQuantityByBillId(id);
                        if (check == "0")
                        {
                            string[][] results = Bdm.getBillDetailByBillId_1(id);

                            foreach (string[] result in results)
                            {
                                if (result != null)
                                {
                                    int sl = book.getQuantityBookByID(Convert.ToInt32(result[0]));
                                    int slm = sl - Convert.ToInt32(result[1]);
                                    book.updateQuanityBook(slm.ToString(), result[0]);
                                }
                            }
                            Bm.updateCheckQuantityByBillId(id);
                        }
                        
                    }
                   
                }
                catch (SystemException se)
                {
                    this.Session["success"] = 2;
                    Response.Redirect("/admin/billdetail/index?bid=" + id);
                   
                }
                if (err == -1)
                {
                    this.Session["success"] = 2;
                    Response.Redirect("/admin/billdetail/index?bid=" + id);
                }
                this.Session["success"] = 1;
                Response.Redirect("/admin/billdetail/index?bid=" + id );
               
            }
            else
            {
                Response.Redirect("/admin/home");
            }
            return View();
        }
       
    }
}
